//@version=5
indicator("Forecast Oscillator w/ cross", shorttitle="FO_cross", overlay=false)

// Input settings
useFixedValue = input.bool(false, title="Fixed Value")
assetType = input.string("forex", title="Asset Type", options=["forex", "crypto", "stocks", "metals"])
timeFrame = input.string("1d", title="Time Frame", options=["4h", "1d"])

// Default Length input
userLength = input.int(14, minval=1, title="Length")

// Price selection input
priceType = input.string("0 - Close", title="Price Type", options=["0 - Close", "1 - Open", "2 - High", "3 - Low", "4 - Median", "5 - Typical", "6 - Weighted"])

// Determine fixed values for Length and Price based on the selected asset type and time frame
var Length = userLength
var Price = priceType

if useFixedValue
    if assetType == "forex" and timeFrame == "1d"
        Length := 69
        Price := "4 - Median"
    else if assetType == "forex" and timeFrame == "4h"
        Length := 76
        Price := "3 - Low"
    else if assetType == "crypto" and timeFrame == "1d"
        Length := 49
        Price := "3 - Low"
    else if assetType == "crypto" and timeFrame == "4h"
        Length := 60
        Price := "6 - Weighted"
    else if assetType == "metals" and timeFrame == "1d"
        Length := 53
        Price := "0 - Close"
    else if assetType == "metals" and timeFrame == "4h"
        Length := 56
        Price := "4 - Median"
    else if assetType == "stocks" and timeFrame == "1d"
        Length := 80
        Price := "0 - Close"
    else if assetType == "stocks" and timeFrame == "4h"
        Length := 70
        Price := "4 - Median"

// Select the appropriate source based on the chosen Price
src = switch Price
    "0 - Close" => close
    "1 - Open" => open
    "2 - High" => high
    "3 - Low" => low
    "4 - Median" => hl2
    "5 - Typical" => hlc3
    "6 - Weighted" => ohlc4
    => close // Default to close if none match

// Forecast Oscillator Calculation
lrc = ta.linreg(src, Length, 0)
lrc1 = ta.linreg(src, Length, 1)
lrs = lrc - lrc1
TSF = ta.linreg(src, Length, 0) + lrs
fosc = 100 * (src - TSF[1]) / src

// Determine color based on position relative to zero
foscColor = fosc > 0 ? color.green : color.red

// Plotting the oscillator with color change
plot(fosc, color=foscColor, linewidth=2, title="Forecast Oscillator")
hline(0, "Zero Line", color=color.blue, linewidth=1)

// Crossover/Crossunder for Forecast Oscillator
plotshape(ta.crossover(fosc, 0), title="FO Crossover", location=location.bottom, style=shape.triangleup, size=size.tiny, color=color.green)
plotshape(ta.crossunder(fosc, 0), title="FO Crossunder", location=location.bottom, style=shape.triangledown, size=size.tiny, color=color.red)
